<?php
ob_start();

session_start();
include('./inst.conf.php');
include('./functions.php');
//for ($i = 0; $i <= 170000000; $i++) {} //vertraging om het load efect te testen

if(isset($_GET['logoff'])){session_destroy();header("location: ".$_SERVER['PHP_SELF']."");}
if (!isset($_SESSION['LOGIN_USERNAME'] ) || !isset( $_SESSION['LOGIN_PASSWORD'] )){   
	echo "Geen toegang.. log in A.U.B.";
	echo "<script>";
	echo " window.location=\"?logoff\";";
	echo "</script>";
	exit;

}
ob_end_flush();
//for ($i = 0; $i <= 17000000; $i++) {} //vertraging om het load efect te testen
$COHORT = @$_GET['COHORT'];
$GROEP = @$_GET['GROEP']; 
$LEERLING_ID =@$_GET['LEERLING_ID'];
$RAPPORT_NR = @$_GET['RAPPORT_NR'];
if(isset($_POST['list_raport'])){$RAPPORT_NR =$_POST['list_raport'];}

if (!isset($_GET['LEERLING_ID']) || $_GET['LEERLING_ID'] == 0){
	$gegevens = mysql_query("SELECT t1.LEERLING_ID,t1.LEERLINGNUMMER,t1.VOORNAAM,t1.TUSSENVOEGSEL,t1.ACHTERNAAM,t3.IK,t3.WERK,t3.KINDEREN, t3.VOLWASSENEN, t3.WERKPUNTEN
							 FROM LEERLING AS t1
							 INNER JOIN RAPPORT AS t2 ON ( t1.LEERLING_ID = t2.LEERLING_ID ) 
							 LEFT JOIN GEDRAG AS t3 ON ( t1.LEERLING_ID = t3.LEERLING_ID ) AND ( t2.GROEP_ID = t3.GROEP_ID ) AND (t3.RAPPORT_NR='$RAPPORT_NR')
							 WHERE t2.RAPPORT_NR = '0' AND t2.groep_id=".@$_GET['GROEP']);
}else{
	$gegevens = mysql_query("SELECT t1.LEERLING_ID,t1.LEERLINGNUMMER,t1.VOORNAAM,t1.TUSSENVOEGSEL,t1.ACHTERNAAM,t3.IK,t3.WERK,t3.KINDEREN, t3.VOLWASSENEN, t3.WERKPUNTEN 
							 FROM LEERLING AS t1
							 INNER JOIN RAPPORT AS t2 ON ( t1.LEERLING_ID = t2.LEERLING_ID ) 
							 LEFT JOIN GEDRAG AS t3 ON ( t1.LEERLING_ID = t3.LEERLING_ID ) AND ( t2.GROEP_ID = t3.GROEP_ID ) AND (t3.RAPPORT_NR='$RAPPORT_NR')
							 WHERE t2.RAPPORT_NR = '0' AND t2.groep_id=".@$_GET['GROEP']." AND t1.LEERLING_ID=" . @$_GET['LEERLING_ID']);
}
if (isset($_POST['list_cohort'])){
	foreach ($_POST as $key => $value) {
		if (substr($key, 0, 2)=="ll"){
			$LEERLING_ID= substr($key, 3);
			$ik = array();$werk = array();$kinderen = array(); $volwassenen = array();$werkpunten = array();
			foreach ($_POST[$key] as $key => $value) {
				//echo "key: $key => value: $value\n\t";
				if(@$key == "ik"){
					foreach ($value as $key => $value) {
						$ik[] = $value;
					}
				}elseif($key == "werk"){
					foreach ($value as $key => $value) {
						$werk[] = $value;
					}
				}elseif($key == "kinderen"){
					foreach ($value as $key => $value) {
						$kinderen[] = $value;
					}
				}elseif($key == "volwassenen"){
					foreach ($value as $key => $value) {
						$volwassenen[] = $value;
					}
				}elseif($key == "werkpunt"){
					foreach ($value as $key => $value) {
						if($value <> ""){$werkpunten[] = SpecialsCharEncode($value);}
					}
				}
			}
			//converter array's
			$ik          = serialize($ik);
			$werk        = serialize($werk);
			$kinderen    = serialize($kinderen);
			$volwassenen = serialize($volwassenen);
			$werkpunten  = serialize($werkpunten);
			//check of de er al gegevens in de DB staat
			$database = mysql_query("SELECT t1.LEERLING_ID,t1.LEERLINGNUMMER,t1.VOORNAAM,t1.TUSSENVOEGSEL,t1.ACHTERNAAM,t3.IK,t3.WERK,t3.KINDEREN, t3.VOLWASSENEN, t3.WERKPUNTEN 
									 FROM LEERLING AS t1
									 INNER JOIN RAPPORT AS t2 ON ( t1.LEERLING_ID = t2.LEERLING_ID ) 
									 LEFT JOIN GEDRAG AS t3 ON ( t1.LEERLING_ID = t3.LEERLING_ID ) AND ( t2.GROEP_ID = t3.GROEP_ID ) AND (t3.RAPPORT_NR='$RAPPORT_NR')
									 WHERE t2.RAPPORT_NR = '0' AND t2.groep_id=".@$_POST['list_groep']." AND t1.LEERLING_ID=" . @$LEERLING_ID);
			
			$database = mysql_fetch_array($database);
			if($database['IK'] == NULL and $database['WERK'] == NULL and $database['KINDEREN'] == NULL and $database['VOLWASSENEN'] == NULL and $database['WERKPUNTEN'] == NULL){
				mysql_query("INSERT INTO `GEDRAG` (`LEERLING_ID`, `GROEP_ID`, `RAPPORT_NR`, `Ik`, `Werk`, `Kinderen`, `Volwassenen`, `Werkpunten`) 
							  VALUES ('$LEERLING_ID', '".$_POST['list_groep']."', '$RAPPORT_NR', '$ik', '$werk', '$kinderen', '$volwassenen', '$werkpunten')");
			}else{
				mysql_query("UPDATE `GEDRAG` SET `Ik` = '$ik',`Werk` = '$werk',`Kinderen` = '$kinderen',`Volwassenen` = '$volwassenen',`Werkpunten` = '$werkpunten' WHERE `LEERLING_ID` ='$LEERLING_ID' AND `GROEP_ID` ='".$_POST['list_groep']."' AND `RAPPORT_NR` = '$RAPPORT_NR'");
			}
		
		} 
	}
	echo "<script>";
	echo "\$('#Opgeslagen').fadeIn(500).delay(10000).fadeOut(400);";
	echo "\$(\"#cijferinvoer\").fadeOut(100, function() {";
	echo "	addLoadDetails('Gegevens zijn opgeslagen in de database<br>');";
	echo "	addLoadDetails('Nieuwe gegevens ophalen...');";
	echo "		\$(\"#cijferinvoer\").load(\"./src/cirkel.invoer.php?COHORT=".$_POST['list_cohort']."&GROEP=".$_POST['list_groep']."&LEERLING_ID=".$_POST['list_leerling']."&RAPPORT_NR=".$RAPPORT_NR."\", function(response, status, xhr) {";
	echo "				if (status == \"error\") {";
	echo "				var msg = \"Sorry but there was an error: \";";
	echo "				\$(\"#error\").html(msg + xhr.status + \" \" + xhr.statusText);";
	echo "					hideLoader();";
	echo "					$.notify({text: 'Er ging iets mis bij het ophalen van de gegevens:<br>'+ xhr.status + ' ' + xhr.statusText , title:'Fout', icon:'images/dialog-error.png'});";
	echo "				}else{";
	echo "					hideLoader();";
	echo "					$.notify({text: 'De door u ingevoerde gegevens zijn opgeslagen', title:'Opgeslagen', icon:'images/dialog-done.png'});";
	echo "				}";
	echo "		}).fadeIn(200);";
	echo "	";
	echo "});"; 
	echo "</script>";
	exit;
}
?>
<script type="text/javascript">

function boxchk_ik(elname, currel) {
	var i=0, list=0;
	
	for (i=0;i<=<?=count($cfg['general']['eigenschappen']['ik']);?>;i++)
	{
		if($('#'+elname+'_'+i).is(':checked')){
			list = list+1;
		}
	}
	if(list >= (<?=$cfg['general']['eigenschappen']['max_select'];?>+1)){
		$.notify({text: 'U mag maximaak <?=$cfg['general']['eigenschappen']['max_select'];?> eigenschappen aanvinken', title:'Info', icon:'images/dialog-information.png'});
		$(currel).attr('checked', false);
	}
	//alert (list);
	
}
function boxchk_werk(elname, currel) {
	var i=0, list=0;
	
	for (i=0;i<=<?=count($cfg['general']['eigenschappen']['werk']);?>;i++)
	{
		if($('#'+elname+'_'+i).is(':checked')){
			list = list+1;
		}
	}
	if(list >= (<?=$cfg['general']['eigenschappen']['max_select'];?>+1)){
		$.notify({text: 'U mag maximaak <?=$cfg['general']['eigenschappen']['max_select'];?> eigenschappen aanvinken', title:'Info', icon:'images/dialog-information.png'});
		$(currel).attr('checked', false);
	}
	//alert (list);
	
}
function boxchk_kinderen(elname, currel) {
	var i=0, list=0;
	
	for (i=0;i<=<?=count($cfg['general']['eigenschappen']['kinderen']);?>;i++)
	{
		if($('#'+elname+'_'+i).is(':checked')){
			list = list+1;
		}
	}
	if(list >= (<?=$cfg['general']['eigenschappen']['max_select'];?>+1)){
		$.notify({text: 'U mag maximaak <?=$cfg['general']['eigenschappen']['max_select'];?> eigenschappen aanvinken', title:'Info', icon:'images/dialog-information.png'});
		$(currel).attr('checked', false);
	}
	//alert (list);
	
}
function boxchk_volwassenen(elname, currel) {
	var i=0, list=0;
	
	for (i=0;i<=<?=count($cfg['general']['eigenschappen']['volwassenen']);?>;i++)
	{
		if($('#'+elname+'_'+i).is(':checked')){
			list = list+1;
		}
	}
	if(list >= (<?=$cfg['general']['eigenschappen']['max_select'];?>+1)){
		$.notify({text: 'U mag maximaak <?=$cfg['general']['eigenschappen']['max_select'];?> eigenschappen aanvinken', title:'Info', icon:'images/dialog-information.png'});
		$(currel).attr('checked', false);
	}
	//alert (list);
	
}
function AddWerkpunt(llID){
	if ($('#txt_'+llID).val() == ""){
		$.notify({text: 'u moet wel text invoeren', title:'Info', icon:'images/dialog-information.png'});
		
	}else{
		var currentTime = new Date();
		var uniqueID=currentTime.getTime();
		$('#T_'+llID).append('<tr id="wrkpnt_'+llID+'_'+uniqueID+'" style="display:none;"><td style="padding:1px;border:0px;"><div class="but-smal"><a href="javascript:void(\'0\');" class="negative" name="info" title="verwijder" onclick="$(this).closest(\'tr\').hide(\'fast\', function(f){$(this).closest(\'tr\').remove()});"><img src="images/icon-delete_trash.png" alt="X"/></a></div></td><td style="border:0px;">'+$('#txt_'+llID).val()+'<input type="hidden" name="ll_'+llID+'[werkpunt][]" value="'+$('#txt_'+llID).val()+'"></td></tr>');
//\"ll_".$data['LEERLING_ID']."[ik][".$key."]\" 
		$('#wrkpnt_'+llID+'_'+uniqueID).show('fast',function(f){$("#div_"+llID).animate({ scrollTop:   $('#T_'+llID).height() }, 300);});
		
		$('#txt_'+llID).val('');
	}
}
</script>

<? 
echo "<table id=\"table_pages\" name=\"pages\" class=\"tablesorder\" width=\"100%\">";
echo "<thead>";
echo "<tr><th>Naam</th><th>Ik</th><th>Werk</th><th>kinderen</th><th>Volwassenen</th><th>Waar jij nog aan kunt werken.</td></tr>";
echo "</thead>";
echo "<tbody>";
while($data = mysql_fetch_assoc($gegevens)){
	$chk_ik="";
	$chk_werk="";
	$chk_kinderen="";
	$chk_volwassenen="";
	$txt_werkpunten="";
	$ik          = array();
	$werk        = array();
	$kinderen    = array();
	$volwassenen = array();
	$werkpunten  = array();
	$ik          = unserialize($data['IK']);
	$werk        = unserialize($data['WERK']);
	$kinderen    = unserialize($data['KINDEREN']);
	$volwassenen = unserialize($data['VOLWASSENEN']);
	$werkpunten  = unserialize($data['WERKPUNTEN']);
	//lowercase
	$ik = @array_map('strtolower', $ik);
	$werk = @array_map('strtolower', $werk);
	$kinderen = @array_map('strtolower', $kinderen);
	$volwassenen = @array_map('strtolower', $volwassenen);
	
	foreach ($cfg['general']['eigenschappen']['ik'] as $key => $value) {
		
		$chk_ik= $chk_ik ."<label for='ik_".$data['LEERLING_ID']."_".$key."'><input type='checkbox' name=\"ll_".$data['LEERLING_ID']."[ik][".$key."]\" id='ik_".$data['LEERLING_ID']."_".$key."' value='$value' onclick=\"boxchk_ik('ik_".$data['LEERLING_ID']."', this);\"";
		if (is_array($ik)){if (in_array($value, $ik)) {$chk_ik= $chk_ik ."checked";}}
		$chk_ik= $chk_ik .">$value</label><br>";
	}
	foreach ($cfg['general']['eigenschappen']['werk'] as $key => $value) {
		$chk_werk= $chk_werk ."<label for='werk_".$data['LEERLING_ID']."_".$key."'><input type='checkbox' name=\"ll_".$data['LEERLING_ID']."[werk][".$key."]\" id='werk_".$data['LEERLING_ID']."_".$key."' value='$value' onclick=\"boxchk_werk('werk_".$data['LEERLING_ID']."', this);\"";
		if (is_array($werk)){if (in_array($value, $werk)) {$chk_werk= $chk_werk ."checked";}}
		$chk_werk= $chk_werk .">$value</label><br>";
	}
	foreach ($cfg['general']['eigenschappen']['kinderen'] as $key => $value) {
		$chk_kinderen= $chk_kinderen ."<label for='kinderen_".$data['LEERLING_ID']."_".$key."'><input type='checkbox' name=\"ll_".$data['LEERLING_ID']."[kinderen][".$key."]\" id='kinderen_".$data['LEERLING_ID']."_".$key."' value='$value' onclick=\"boxchk_kinderen('kinderen_".$data['LEERLING_ID']."', this);\"";
		if (is_array($kinderen)){if (in_array($value, $kinderen)) {$chk_kinderen= $chk_kinderen ."checked";}}
		$chk_kinderen= $chk_kinderen .">$value</label><br>";
	}
	foreach ($cfg['general']['eigenschappen']['volwassenen'] as $key => $value) {
		$chk_volwassenen= $chk_volwassenen ."<label for='volwassenen_".$data['LEERLING_ID']."_".$key."'><input type='checkbox' name=\"ll_".$data['LEERLING_ID']."[volwassenen][".$key."]\" id='volwassenen_".$data['LEERLING_ID']."_".$key."' value='$value' onclick=\"boxchk_volwassenen('volwassenen_".$data['LEERLING_ID']."', this);\"";
		if (is_array($volwassenen)){if (in_array($value, $volwassenen)) {$chk_volwassenen= $chk_volwassenen ."checked";}}
		$chk_volwassenen= $chk_volwassenen .">$value</label><br>";
	}
	if (is_array($werkpunten)){
		foreach ($werkpunten as $key => $value) {
			$txt_werkpunten = $txt_werkpunten ."<tr><td style=\"padding:1px;border:0px;\"><div class=\"but-smal\"><a href=\"javascript:void('0');\" class=\"negative\" name=\"info\" title=\"verwijder\" onclick=\"$(this).closest('tr').hide('fast', function(f){\$(this).closest('tr').remove()});\"><img src=\"images/icon-delete_trash.png\" alt=\"X\"/></a></div></td><td style=\"border:0px;\">".stripslashes(SpecialsCharDecode($value))."<input type=\"hidden\" name=\"ll_".$data['LEERLING_ID']."[werkpunt][]\" value=\"". stripslashes(SpecialsCharDecode($value))."\"></td></tr>";
		}
	}
	if ($data['TUSSENVOEGSEL'] == ""){$naam = utf8_encode($data['VOORNAAM'] ." ". $data['ACHTERNAAM']);}else{$naam = utf8_encode($data['VOORNAAM'] ." ". $data['TUSSENVOEGSEL'] ." ".  $data['ACHTERNAAM']);}
	echo "<tr><th>$naam</th><td><div style='height:125px;overflow:auto;'>$chk_ik</div></td><td><div style='height:125px;overflow:auto;'>$chk_werk</div></td><td><div style='height:125px;overflow:auto;'>$chk_kinderen</div></td><td><div style='height:125px;overflow:auto;'>$chk_volwassenen</div></td>";
	//<div id="myDiv" style="width: 500px; height: 100px; overflow: auto; border: 1px solid black;"></div>
	echo "<td width=\"200px\"><div style='height:100px;overflow:auto;' id=\"div_".$data['LEERLING_ID']."\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"padding:0px;\" id=\"T_".$data['LEERLING_ID']."\">$txt_werkpunten</table></div><input type=\"text\" style=\"float:left;top:10px;width:160px;height:25px;\" id=\"txt_".$data['LEERLING_ID']."\" onkeypress=\"{if (event.keyCode==13)AddWerkpunt(".$data['LEERLING_ID'].")}\" name=\"ll_".$data['LEERLING_ID']."[werkpunt][]\"><div class=\"but-smal\"><a href=\"javascript:void('0');\" class=\"neutral\" name=\"info\" id=\"save_butt\" title=\"Toevoegen\" onclick=\"AddWerkpunt(".$data['LEERLING_ID'].");\"><img src=\"images/icon-comment.png\" alt=\"\"/></a></div></td></tr>";
}

//icon-delete_trash.png
?>


	
